package command;

/**
 * User: Alexander Martyushov
 * Date: 12/10/12
 * Time: 7:38 AM
 */
public class RealRemoteControl {

    Command[] onCommands;
    Command[] offCommands;
    Command undoCommand;

    public RealRemoteControl(){
        onCommands = new Command[7];
        offCommands = new Command[7];

        Command noCommand = new NoCommand();
        for (int i = 0; i < 7; i++){
            onCommands[i] = noCommand;
            offCommands[i] = noCommand;
        }
        undoCommand = new NoCommand();
    }

    public void setCommand(int slot, Command onCommand, Command offCommand){
        onCommands[slot] = onCommand;
        offCommands[slot] = offCommand;
    }

    public void onButtonWasPushed(int slot){
        if (onCommands[slot] != null){
        onCommands[slot].execute();   }

        undoCommand = onCommands[slot];
    }

    public void offButtonWasPushed(int slot){
        offCommands[slot].execute();
        undoCommand = offCommands[slot];
    }

    public void undButtonWasPressed(){
        undoCommand.undo();
    }

    public String toString(){
        StringBuffer stringBuff = new StringBuffer();
        stringBuff.append("\n----------Remote Control------------\n");
        for (int i=0; i<onCommands.length; i++){
            stringBuff.append("[slot " + i + "] " + onCommands[i].getClass().getName() + "  "
                    + offCommands[i].getClass().getName() + "\n");
        }
        return stringBuff.toString();
    }

}
